//
//  DetailViewController.m
//  SoutheastAsia
//
//  Created by mEnder on 16/1/15.
//  Copyright © 2016年 zlj. All rights reserved.
//

#import "DetailSceneryViewController.h"
#import "WeatherViewController.h"
#import "DetailSceneryTableViewCell.h"
#import "SceneryModel.h"
#import "TravelnotesViewController.h"

@interface DetailSceneryViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIButton *weatherButton;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) SceneryModel *sceneryModel;

@end

@implementation DetailSceneryViewController

- (void)dealloc {
    [self removeObserver];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.weatherButton];
    [self.view addSubview:self.backButton];
    [self.view addSubview:self.titleLabel];
    self.titleLabel.text = self.city;
    [self.sceneryModel loadNoteListWithCity:self.city];
    [self registerObserver];
}

#pragma mark -- private methods

- (void)registerObserver {
    [self.sceneryModel addObserver:self forKeyPath:@"noteList" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)removeObserver {
    [self.sceneryModel removeObserver:self forKeyPath:@"noteList"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    self.dataSource = self.sceneryModel.noteList;
    [self.tableView reloadData];
}

#pragma mark -- action/callback

- (void)weatherButtonTaped:(UIButton *)sender {
    WeatherViewController *weatherVC = [[WeatherViewController alloc] init];
    weatherVC.countryIndex = self.indexOfSection;
    weatherVC.city = self.city;
    [self presentViewController:weatherVC animated:YES completion:nil];
}

- (void)backButtonTaped:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailSceneryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailSceneryTableViewCell"];
    [cell loadNoteList:self.dataSource[indexPath.row]];
    return cell;
}

#pragma mark -- UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    TravelnotesViewController *travelNotesVC = [[TravelnotesViewController alloc] init];
    travelNotesVC.urlString = self.dataSource[indexPath.row][@"bookUrl"];
    travelNotesVC.noteTitle = self.dataSource[indexPath.row][@"title"];
    [self.navigationController pushViewController:travelNotesVC animated:YES];
}

#pragma mark -- getter

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = ({
            UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 80) style:UITableViewStylePlain];
            tableView.backgroundColor = [UIColor whiteColor];
            tableView.delegate = self;
            tableView.dataSource = self;
            [tableView registerClass:[DetailSceneryTableViewCell class] forCellReuseIdentifier:@"DetailSceneryTableViewCell"];
            tableView.rowHeight = 100;
            tableView.tableFooterView = [[UIView alloc] init];
            tableView;
        });
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (UIButton *)weatherButton {
    if (!_weatherButton) {
        _weatherButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(CGRectGetWidth(self.view.bounds) - 75, 38, 75, 30);
            [button setBackgroundImage:[UIImage imageNamed:@"天气.png"] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:20];
            [button addTarget:self action:@selector(weatherButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _weatherButton;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(10, 30, 60, 40);
            [button setImage:[UIImage imageNamed:@"返回按钮.png"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(backButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _backButton;
}

- (SceneryModel *)sceneryModel {
    if (!_sceneryModel) {
        _sceneryModel = [[SceneryModel alloc] init];
    }
    return _sceneryModel;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.bounds = CGRectMake(0, 0, 200, 300);
        _titleLabel.center = CGPointMake(CGRectGetMidX(self.view.bounds), 50);
        _titleLabel.font = [UIFont systemFontOfSize:24];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

@end
